<?php
namespace Home\Controller;
use Think\Log;

/**
 * 异步提交
 * User: linkwhy
 * Date: 2015/10/28
 * Time: 11:53
 */
class PayNotifyController extends HomeController{

    /**
     *
     */
    public function index(){
        //ORDER_KEY 为服务器提供的加密字符
        ksort($_POST);//升序排列参数
        $str="";
        foreach($_POST as $k=>$v){//链接所有参数和值
            if($k!="sign"){
                $str.=$k.$v;
            }
        }
        //echo $str;
        $sign=md5($str."aaa123456");
        if($sign!=strtolower($_POST["sign"])){
            echo "-1";//签名异常
            exit;
        }

        //订单业务处理...
//        echo "1";//表示处理成功
        $order_id = I('post.out_order_id');
        $row = M('order_item')->alias('item')->field('g.id,class_id,num,is_ppl')
            ->join('__CART__ c ON item.cart_id = c.id', 'left')
            ->join('__GOODS__ g ON c.goods_id = g.id', 'left')
            ->where(array('order_id' => $order_id))->find();
        if( (int)$row['class_id'] == 11){
            $this->makeMoney($order_id, 10990);
            M('goods')->where(array('id' => (int)$row['id']))->setInc('sale_count', (int)$row['num']);
            M('order')->where(array('order_id' => $order_id))->save(array('order_status' => 6, 'pay_type' => 1, 'success_time' => time(), 'pay_time' => time(), 'is_clear' => 1, 'take_time' => time()));
            echo "1";
        }else{
            if(M('order')->where(array('order_id' => $order_id))->save(array('order_status' => 2, 'pay_time' => time(), 'pay_type' => 1))){
                //限购产品
                if( (int)$row['is_ppl'] == 1 ){
                    M('goods')->where(array('id' => (int)$row['id']))->setDec('ppl_num');       //限购数量减少1
                }
                echo "1";
            }
        }

    }

    /**
     * 放款 确认订单完成操作
     */
    public function makeMoney($order_id, $seller_id){

        //参数　order_id：商城提交时的订单id
        //      seller_id:商城点商店铺id
        $arr=array('order_id'=>"$order_id","seller_id"=>"$seller_id");

        ksort($arr);
        $str="";
        foreach($arr as $k=>$v){

            $str.=$k.$v;

        }
        //echo $str;
        $sign=md5($str."aaa123456");
        $arr['sign']=$sign;

        //echo $str."aaa123456";
        //http://120.25.151.137 测试服
        //182.92.31.222/ 正式服
        //返回1代表成功　０代表失败
        return $this->apiPost("http://182.92.31.222/server/index.php?c=Order&a=completeOrderEShop",$arr);
    }

    /**
     * 异步提交
     * @param $uri
     * @param $post_data
     * @return bool
     */
    public function apiPost($uri,$post_data){
        //$post_data = array ("username" => "bob","key" => "12345");
        $ch = curl_init();
        $headr[] ="X-Requested-With:1";
        $headr[] ="UID:0";
        $headr[] ="SESSION:0";
        //$header[]= "Content-Type: application/x-www-form-urlencoded";
        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
        $output = curl_exec($ch);
        $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if($output!="1"){
            echo $output;
            //var_dump($post_data);
            return false;
        }
        //打印获得的数据
        return true;
    }

}